package qe.spider.grab;

import cn.hutool.core.text.UnicodeUtil;
import com.google.gson.*;
import lombok.extern.slf4j.Slf4j;
import okhttp3.FormBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;


//根据板块查个股
@Slf4j
public class GrabPlateStocks {
    private String userID;
    private String token;

    public GrabPlateStocks(String userID, String token) {
        this.userID = userID;
        this.token = token;
    }

    public static void main(String[] args) throws IOException {
        new GrabPlateStocks("111775r", "13f1764f728c8db11f85b98e524bec3c").grab("801033");
    }

    public List<String> grab(String plateId) throws IOException {
        FormBody formBody = new FormBody.Builder()
                .add("c", "PCArrangeData")
                .add("a", "GetZSIndexPlate")
                .add("SelType", "3")
                .add("LType", "6")
                .add("LOrder", "1")
                .add("LStart", "")
                .add("LEnd", "")
                .add("LIndex", "0")
                .add("Lst", "6000")
                .add("PlateID", plateId)
                .add("UserID", userID)
                .add("Token", token)
                .build();

        OkHttpClient client = new OkHttpClient();
        final Request request = new Request.Builder()
                .url("https://pchq.kaipanla.com/w1/api/index.php")
//                .header("Accept-Encoding", "gzip, deflate, br")
                .header("Accept-Encoding", "br")
                .header("Accept-Language", "zh-CN,zh;q=0.9,en;q=0.8,en-GB;q=0.7,en-US;q=0.6")
                .header("Connection", "keep-alive")
                .header("Content-Length", "108")
                .header("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8")
                .header("Host", "pchq.kaipanla.com")
                .header("Origin", "https://www.kaipanla.com")
                .header("Referer", "https://www.kaipanla.com/")
                .header("sec-ch-ua", "\"Not_A Brand\";v=\"99\", \"Microsoft Edge\";v=\"109\", \"Chromium\";v=\"109\"")
                .header("sec-ch-ua-mobile", "?0")
                .header("sec-ch-ua-platform", "\"Windows\"")
                .header("Sec-Fetch-Dest", "empty")
                .header("Sec-Fetch-Mode", "cors")
                .header("Sec-Fetch-Site", "same-site")
                .header("User-Agent", "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/109.0.0.0 Safari/537.36 Edg/109.0.1518.140")
                .post(formBody)
                .build();
        Gson gson = new Gson();
        try (Response response = client.newCall(request).execute()) {
            if (!response.isSuccessful()) throw new IOException("Unexpected code " + response);


            String result = UnicodeUtil.toString(response.body().string());
            log.info(result);

            JsonObject jsonObject = JsonParser.parseString(result).getAsJsonObject().getAsJsonObject("stocks");
            JsonArray jsonArray = jsonObject.getAsJsonArray("list");
            List<String> stockCodes = new ArrayList<>();
            Iterator<JsonElement> it = jsonArray.iterator();
            while (it.hasNext()) {
                JsonArray single = it.next().getAsJsonArray();
                stockCodes.add(single.get(0).getAsString());
            }
            return stockCodes;
        }

    }


}
